﻿Imports MySQL
Imports MySQLCommand
Imports System.Data.SqlClient
Imports System.Configuration

Public Class Admin
    Dim hienthi As Boolean = False
    Dim user As String = ""
    Sub Connect()
        clsSQL.setConnection(ConfigurationSettings.AppSettings("connect"))
        If clsSQL.Connected() = False Then
            MsgBox("Lỗi kết nối")
            'MsgBox("Kết nối thành công")
        End If
    End Sub
    Private Sub Admin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Connect()
        lblUser.Text = MaNVDangNhap
        loadNV()

    End Sub

    Private Sub btnHienPass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHienPass.Click
        hienthi = Not hienthi
        If hienthi = True Then
            txtMatKhau.PasswordChar = ""
        Else
            txtMatKhau.PasswordChar = "*"
        End If
    End Sub

    Function kiemtraNV() As Boolean
        If txtTenDN.Text <> "" Then
            If txtMatKhau.Text <> "" Then
                If txtHoten.Text <> "" Then
                    If txtNgaysinh.Text <> "" Then
                        If txtDienthoai.Text <> "" Then

                            Return True
                        Else
                            MsgBox("Chưa nhập số điện thoại")
                            Return False
                        End If
                    Else
                        MsgBox("Chưa nhập ngày sinh")
                        Return False
                    End If
                Else
                    MsgBox("chưa nhập họ tên")
                    Return False
                End If
            Else
                MsgBox("chưa nhập mật khẩu")
                Return False
            End If
        Else
            MsgBox("Chưa nhập tên đăng nhập ")
            Return False
        End If
    End Function

    Function ktquyen(ByVal ad As Boolean, ByVal nhap As Boolean, ByVal ban As Boolean) As Integer
        If ad Then
            Return 1
        Else
            If nhap Then
                Return 2
            Else
                Return 3
            End If
        End If
    End Function

    Function kttinhtrang(ByVal dc As Boolean, ByVal cam As Boolean) As Integer
        If dc Then
            Return 1
        End If
        Return 0
    End Function

    Sub loadNV()
        DsQLUser.Tables("sp_DanhSachUser").Clear()
        clsSQL.FillDataSet("sp_DanhSachUser", DsQLUser, "sp_DanhSachUser")
        'Dim i As Integer
        'For i = 0 To dtgUser.Rows.Count - 1
        '    If dtgUser.Item(4, i).Value = 1 Then
        '        dtgUser.Item(4, i).Value = "Admin"
        '    Else
        '        If dtgUser.Item(4, i).Value = 2 Then
        '            dtgUser.Item(4, i).Value = "Nhập kho"

        '        Else
        '            dtgUser.Item(4, i).Value = "Xuất kho"
        '        End If
        '    End If
        '    If dtgUser.Item(5, i).Value = 1 Then
        '        dtgUser.Item(5, i).Value = "Dùng"

        '    Else
        '        dtgUser.Item(5, i).Value = "Cấm"
        '    End If
        'Next
    End Sub

    Sub MacDinh()

        txtTenDN.Text = ""
        txtTenDN.ReadOnly = False
        txtHoten.Text = ""
        txtMatKhau.Text = ""
        txtMatKhau.ReadOnly = False
        radNhap.Checked = True
        radSuDung.Checked = True
        txtNgaysinh.Text = ""
        txtDienthoai.Text = ""
        user = ""
        btnThemNV.BackgroundImage = ASIA.My.Resources.Resources.adduser
    End Sub
    Private Sub btnThemNV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemNV.Click
        If user <> "" Then 'update user
            clsSQL.ExecReader("sp_InsertUpdateNV", "u", user, txtMatKhau.Text, txtHoten.Text, ktquyen(radAdmin.Checked, radNhap.Checked, radBan.Checked), kttinhtrang(radSuDung.Checked, radCam.Checked), txtNgaysinh.Text, txtDienthoai.Text)
            'MsgBox("tên đăng nhập là " & txtTenDN.Text & " mật khẩu là " & txtMatKhau.Text & " quyền là " & ktquyen(radAdmin.Checked, radNhap.Checked, radBan.Checked).ToString & " trạng thai la " & kttinhtrang(radSuDung.Checked, radCam.Checked).ToString & " ngày sinh là " & txtNgaysinh.Text & " điện thoại là " & txtDienthoai.Text)
            clsSQL.getData.Read()
            MsgBox(clsSQL.getData("Err"))
            MacDinh()
            loadNV()

        Else ' insert user
        If kiemtraNV() Then
            clsSQL.ExecReader("sp_InsertUpdateNV", "i", txtTenDN.Text, txtMatKhau.Text, txtHoten.Text, ktquyen(radAdmin.Checked, radNhap.Checked, radBan.Checked), kttinhtrang(radSuDung.Checked, radCam.Checked), txtNgaysinh.Text, txtDienthoai.Text)
                clsSQL.getData.Read()
            MsgBox(clsSQL.getData("Err"))
            MacDinh()
            loadNV()
        End If
        End If
    End Sub

    Private Sub btnHuyNV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuyNV.Click
        MacDinh()
    End Sub

    Private Sub dtgUser_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgUser.CellContentClick

    End Sub

    Private Sub dtgUser_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgUser.DoubleClick
        btnThemNV.BackgroundImage = ASIA.My.Resources.Resources.save
        user = dtgUser.Item(0, dtgUser.CurrentRow.Index).Value
        txtTenDN.ReadOnly = True
        txtTenDN.Text = user
        txtMatKhau.ReadOnly = True
        txtHoten.Text = dtgUser.Item(1, dtgUser.CurrentRow.Index).Value.ToString
        txtNgaysinh.Text = dtgUser.Item(2, dtgUser.CurrentRow.Index).Value.ToString
        txtDienthoai.Text = dtgUser.Item(3, dtgUser.CurrentRow.Index).Value.ToString
        If dtgUser.Item(4, dtgUser.CurrentRow.Index).Value = 1 Then
            radAdmin.Checked = True
        Else
            If dtgUser.Item(4, dtgUser.CurrentRow.Index).Value = 2 Then
                radNhap.Checked = True
            Else
                If dtgUser.Item(4, dtgUser.CurrentRow.Index).Value = 3 Then
                    radBan.Checked = True
                End If
            End If
        End If
        If dtgUser.Item(5, dtgUser.CurrentRow.Index).Value = True Then
            radSuDung.Checked = True
        Else
            If dtgUser.Item(5, dtgUser.CurrentRow.Index).Value = False Then
                radCam.Checked = True
            End If
        End If
    End Sub

    Private Sub mniXoaUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mniXoaUser.Click
        clsSQL.ExecReader("sp_DeleteNV", dtgUser.Item(0, dtgUser.CurrentRow.Index).Value)
        clsSQL.getData.Read()
        If clsSQL.getData("Err") = "tc" Then
            MsgBox("Đã xóa thành công")
        Else
            MsgBox("User này có tham gia lập hóa đơn. Không xóa được")
        End If
        clsSQL.getData.Close()
        loadNV()
    End Sub

    Private Sub mniXoaPass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mniXoaPass.Click
        clsSQL.ExecCommand("sp_Resetpass", dtgUser.Item(0, dtgUser.CurrentRow.Index).Value)
        MsgBox("Đã xóa pass thành công")
        loadNV()
    End Sub

    Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
        If dtgUser.Rows.Count <> 0 Then
            mniXoaUser.Enabled = True
            mniXoaPass.Enabled = True
        Else
            mniXoaUser.Enabled = False
            mniXoaPass.Enabled = False
        End If
    End Sub
End Class